package org.compiere.model;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class Mhrmilness extends X_hrm_illness {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6229505199034373757L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmilness.class);

	public Mhrmilness(Properties ctx, int hrm_illness_ID, String trxName) {
		super(ctx, hrm_illness_ID, trxName);
		// 
	}

	public Mhrmilness(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		//
	}
	
	/** Select average salary for count ills */
	public static Double getAverageSalary(int C_BPartner_ID, Timestamp dt) {
		// 
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Double vAverageSalary = .0;
			
		/** Average salary select */
		String sql_ = "SELECT (MAX(TOTALIND)/MAX(WORKDAYS))::NUMERIC(10,2) FROM HRM_AVERAGESALARY_V \n " +						
						"WHERE AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							"AND TO_DATE('01-'||TO_CHAR(DATEFROM,'YYYY-MM'),'DD.YYYY.MM') <=" + DB.TO_DATE(dt) + " \n " +
							"AND TO_DATE('31-'||TO_CHAR(DATEFROM,'YYYY-MM'),'DD.YYYY.MM') >=" + DB.TO_DATE(dt);
			try
			{
				pstmt = DB.prepareStatement(sql_,null);				
				rs = pstmt.executeQuery();	
				if(rs.next())
					vAverageSalary = rs.getDouble(1);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return vAverageSalary;
	}

}
